package com.mohe.dangjian.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.model.CommunityProject;
import com.mohe.dangjian.model.CommunityProjectActivity;
import com.mohe.dangjian.service.interfaces.ICommunityProject;
import com.mohe.dangjian.service.interfaces.ICommunityProjectActivity;
import com.mohe.dangjian.utils.Json;
import com.mohe.dangjian.utils.JsonUtil;
import com.mohe.dangjian.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 居民区项目活动Action
 * 
 * @author wxm
 * 
 */
public class CommunityProjectActivityAction extends ActionSupport {

	private static final long serialVersionUID = 1089168662805047807L;

	private ICommunityProjectActivity communityProjectActivityServ;
	private ICommunityProject communityProjectServ;
	
	private List<CommunityProjectActivity> communityProjectActivityList;
	private List<CommunityProject> communityProjectList;
	private Page page = new Page();
	private CommunityProjectActivity communityProjectActivity = new CommunityProjectActivity();

	private String flag;
	private Long projectKey;
	// 表示从哪跳转到编辑页面，1:主页面/2:查看页面
	private Integer editFrom;

	public ICommunityProjectActivity getCommunityProjectActivityServ() {
		return communityProjectActivityServ;
	}

	public void setCommunityProjectActivityServ(
			ICommunityProjectActivity communityProjectActivityServ) {
		this.communityProjectActivityServ = communityProjectActivityServ;
	}

	public ICommunityProject getCommunityProjectServ() {
		return communityProjectServ;
	}

	public void setCommunityProjectServ(ICommunityProject communityProjectServ) {
		this.communityProjectServ = communityProjectServ;
	}

	public List<CommunityProjectActivity> getCommunityProjectActivityList() {
		return communityProjectActivityList;
	}

	public void setCommunityProjectActivityList(
			List<CommunityProjectActivity> communityProjectActivityList) {
		this.communityProjectActivityList = communityProjectActivityList;
	}

	public List<CommunityProject> getCommunityProjectList() {
		return communityProjectList;
	}

	public void setCommunityProjectList(List<CommunityProject> communityProjectList) {
		this.communityProjectList = communityProjectList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public CommunityProjectActivity getCommunityProjectActivity() {
		return communityProjectActivity;
	}

	public void setCommunityProjectActivity(CommunityProjectActivity communityProjectActivity) {
		this.communityProjectActivity = communityProjectActivity;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Long getProjectKey() {
		return projectKey;
	}

	public void setProjectKey(Long projectKey) {
		this.projectKey = projectKey;
	}

	public Integer getEditFrom() {
		return editFrom;
	}

	public void setEditFrom(Integer editFrom) {
		this.editFrom = editFrom;
	}

	/**
	 * 获取项目信息列表
	 * @return
	 */
	public String getProjectActivityListForPage() {
		int count = communityProjectActivityServ.getCount(communityProjectActivity);
		page.setCount(count);
		communityProjectActivityList = communityProjectActivityServ.getCommunityProjectActivityList(communityProjectActivity, page);
		return "projectActivity.jsp";
	}
	
	/**
	 * 分页
	 */
	public void searchProjectActivityList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = communityProjectActivityServ.getCount(communityProjectActivity);
		page.setCount(count);
		List<CommunityProjectActivity> list = communityProjectActivityServ.getCommunityProjectActivityList(communityProjectActivity, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (CommunityProjectActivity s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getName())) {
					map.put("name", s.getName());
				} else {
					map.put("name", "");
				}
				if(s.getCommunityProject() != null && !StringUtils.isBlank(s.getCommunityProject().getName())){
					map.put("project", s.getCommunityProject().getName());
				} else {
					map.put("project", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 跳转到编辑界面
	 * @return
	 */
	public String toEditProjectActivity() {
		communityProjectActivity = communityProjectActivityServ.getCommunityProjectActivityById(communityProjectActivity.getTimeKey());
		communityProjectList = communityProjectServ.getAllCommunityProject();
		projectKey = communityProjectActivity.getCommunityProject().getTimeKey();
		return "editProjectActivity.jsp";
	}
	
	/**
	 * 跳转到新增界面
	 * @return
	 */
	public String toAddProjectActivity() {
		communityProjectList = communityProjectServ.getAllCommunityProject();
		return "addProjectActivity.jsp";
	}
	
	/**
	 * 更新项目信息
	 */
	public void updateProjectActivity(){
//		Json json = new Json();
//		communityProjectActivity.setCreateDate(new Date());
//		CommunityProject project = communityProjectServ.getCommunityProjectById(projectKey);
//		communityProjectActivity.setCommunityProject(project);
//		boolean result = communityProjectActivityServ.updateCommunityProjectActivity(communityProjectActivity);
//		if (result) {
//			json.setMessage("编辑成功");
//		} else {
//			json.setSuccess(true);
//			json.setMessage("编辑失败");
//		}
//		JsonUtil.writeJson(json);
		
		Json json = new Json();
		CommunityProjectActivity newPrjAct = communityProjectActivityServ.getCommunityProjectActivityById(communityProjectActivity.getTimeKey());
		newPrjAct.setName(communityProjectActivity.getName());
		newPrjAct.setContent(communityProjectActivity.getContent());
		newPrjAct.setSituation(communityProjectActivity.getSituation());
		newPrjAct.setCreateDate(new Date());
		CommunityProject project = communityProjectServ.getCommunityProjectById(projectKey);
		newPrjAct.setCommunityProject(project);
		boolean result = communityProjectActivityServ.updateCommunityProjectActivity(newPrjAct);
		if (result) {
			json.setMessage("编辑成功");
		} else {
			json.setSuccess(true);
			json.setMessage("编辑失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 保存新增项目
	 */
	public void saveProjectActivity() {
		Json json = new Json();
		CommunityProject project = communityProjectServ.getCommunityProjectById(projectKey);
		communityProjectActivity.setCommunityProject(project);
		boolean result = communityProjectActivityServ.saveCommunityProjectActivity(communityProjectActivity);
		if (result) {
			json.setMessage("新增成功");
		} else {
			json.setSuccess(true);
			json.setMessage("新增失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 删除项目
	 */
	public void delProjectActivity() {
		Json json = new Json();
		boolean result = communityProjectActivityServ.delCommunityProjectActivity(communityProjectActivity.getTimeKey());
		if (result) {
			json.setMessage("删除成功");
		} else {
			json.setSuccess(true);
			json.setMessage("删除失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String toShowProjectActivity(){
		communityProjectActivity = communityProjectActivityServ.getCommunityProjectActivityById(communityProjectActivity.getTimeKey());
		return "showProjectActivity.jsp";
	}
}
